package Algorithms;

public class BubbleSort {
	
	int []arrayToSort;
	int length;
	boolean swapped = false;
	
	public void sort(int []array){
		
		this.arrayToSort = array;
		this.length = array.length;
		bubblesort();
	}
	
	private void bubblesort(){
		
		do{
			swapped = false;
			for (int i = 1; i < length; i++){
				if (arrayToSort[i-1] > arrayToSort[i]){
					swap(i-1, i);
					swapped = true;
				}
			}
		}while (swapped);
	}
	
	private void swap(int i, int i2) {
		
		int temp = arrayToSort[i];
		arrayToSort[i] = arrayToSort[i2];
		arrayToSort[i2] = temp;
	}
	
	public void printRes(){
		
		for(int i = 0; i < length; i++)
			System.out.println(arrayToSort[i]);
	}
}
